package efr.dao.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import efr.dao.EditorialDao;
import efr.entity.Editorial;
import efr.util.HibernateUtil;

public class EditorialDaoImpl implements EditorialDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Editorial> getAllEditoriale() {
		List<Editorial> listEditoriale = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		listEditoriale = session.createQuery("FROM Editorial").list();
		for (Editorial editorial : listEditoriale) {
			Hibernate.initialize(editorial.getAutor());
		}
		session.getTransaction().commit();
		return listEditoriale;
	}

	@Override
	public Editorial getEditorial(int id) {
		Editorial editorial = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		editorial = (Editorial)session.get(Editorial.class, id);
		Hibernate.initialize(editorial.getAutor());
		session.getTransaction().commit();
		return editorial;
	}

	@Override
	public void updatedEditorial(Editorial editorial) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.update(editorial);
		session.getTransaction().commit();
		
	}

	@Override
	public Integer addEditorial(Editorial editorial) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Integer id = (Integer)session.save(editorial);
		session.getTransaction().commit();
		return id;
	}

	@Override
	public void deleteEditorial(Editorial editorial) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(editorial);
		session.getTransaction().commit();
	}
}
